import { SetMetadata } from '@nestjs/common'

/**
 * 日志级别枚举
 */
export enum LogLevel {
  DEBUG = 'debug',
  INFO = 'info',
  WARN = 'warn',
  ERROR = 'error'
}

/**
 * 日志配置接口
 */
export interface LogConfig {
  level?: LogLevel
  message?: string
  includeArgs?: boolean
  includeResult?: boolean
  includeExecutionTime?: boolean
  sensitiveFields?: string[]
}

/**
 * 日志元数据键
 */
export const LOG_METADATA_KEY = 'log_config'

/**
 * 日志装饰器
 * 用于标记需要记录日志的方法
 *
 * @param config 日志配置
 *
 * @example
 * ```typescript
 * @Log({ level: LogLevel.INFO, includeArgs: true })
 * async createUser(userData: CreateUserDto) {
 *   // 方法实现
 * }
 *
 * @Log({
 *   level: LogLevel.WARN,
 *   message: 'Sensitive operation performed',
 *   sensitiveFields: ['password', 'token']
 * })
 * async updatePassword(userId: string, newPassword: string) {
 *   // 方法实现
 * }
 * ```
 */
export const Log = (config: LogConfig = {}) => {
  const defaultConfig: LogConfig = {
    level: LogLevel.INFO,
    includeArgs: false,
    includeResult: false,
    includeExecutionTime: true,
    sensitiveFields: ['password', 'token', 'secret', 'key'],
    ...config
  }

  return SetMetadata(LOG_METADATA_KEY, defaultConfig)
}

/**
 * 审计日志装饰器
 * 用于记录重要的业务操作
 *
 * @param operation 操作名称
 * @param resource 资源类型
 *
 * @example
 * ```typescript
 * @Audit('CREATE_USER', 'User')
 * async createUser(userData: CreateUserDto) {
 *   // 方法实现
 * }
 *
 * @Audit('DELETE_ARTICLE', 'Article')
 * async deleteArticle(articleId: string) {
 *   // 方法实现
 * }
 * ```
 */
export const Audit = (operation: string, resource: string) => {
  return SetMetadata('audit_config', {
    operation,
    resource,
    level: LogLevel.INFO,
    includeArgs: true,
    includeResult: true,
    includeExecutionTime: true
  })
}

/**
 * 性能监控装饰器
 * 用于监控方法执行性能
 *
 * @param threshold 性能阈值（毫秒）
 * @param operation 操作名称
 *
 * @example
 * ```typescript
 * @Performance(1000, 'Database Query')
 * async findUsers(query: FindUsersDto) {
 *   // 方法实现
 * }
 *
 * @Performance(500, 'File Upload')
 * async uploadFile(file: Express.Multer.File) {
 *   // 方法实现
 * }
 * ```
 */
export const Performance = (threshold: number = 1000, operation?: string) => {
  return SetMetadata('performance_config', {
    threshold,
    operation,
    level: LogLevel.WARN,
    includeExecutionTime: true
  })
}

/**
 * 安全日志装饰器
 * 用于记录安全相关操作
 *
 * @param severity 安全级别
 * @param event 安全事件名称
 *
 * @example
 * ```typescript
 * @Security('high', 'LOGIN_ATTEMPT')
 * async login(credentials: LoginDto) {
 *   // 方法实现
 * }
 *
 * @Security('critical', 'ADMIN_ACCESS')
 * async deleteAllUsers() {
 *   // 方法实现
 * }
 * ```
 */
export const Security = (severity: 'low' | 'medium' | 'high' | 'critical', event: string) => {
  return SetMetadata('security_config', {
    severity,
    event,
    level: LogLevel.WARN,
    includeArgs: true,
    includeResult: false,
    includeExecutionTime: true
  })
}

/**
 * 数据库操作日志装饰器
 * 用于记录数据库操作
 *
 * @param table 表名
 * @param operation 操作类型
 *
 * @example
 * ```typescript
 * @Database('users', 'CREATE')
 * async createUser(userData: CreateUserDto) {
 *   // 方法实现
 * }
 *
 * @Database('articles', 'UPDATE')
 * async updateArticle(id: string, data: UpdateArticleDto) {
 *   // 方法实现
 * }
 * ```
 */
export const Database = (table: string, operation: 'CREATE' | 'READ' | 'UPDATE' | 'DELETE') => {
  return SetMetadata('database_config', {
    table,
    operation,
    level: LogLevel.DEBUG,
    includeExecutionTime: true
  })
}

/**
 * 文件操作日志装饰器
 * 用于记录文件操作
 *
 * @param operation 文件操作类型
 *
 * @example
 * ```typescript
 * @FileOperation('UPLOAD')
 * async uploadFile(file: Express.Multer.File) {
 *   // 方法实现
 * }
 *
 * @FileOperation('DELETE')
 * async deleteFile(fileId: string) {
 *   // 方法实现
 * }
 * ```
 */
export const FileOperation = (operation: 'UPLOAD' | 'DOWNLOAD' | 'DELETE' | 'MOVE' | 'COPY') => {
  return SetMetadata('file_operation_config', {
    operation,
    level: LogLevel.INFO,
    includeArgs: true,
    includeResult: true,
    includeExecutionTime: true
  })
}
